package org.diffshop.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.to.MemberPrice;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;
import org.diffshop.coupon.dao.MemberPriceDao;
import org.diffshop.coupon.entity.MemberPriceEntity;
import org.diffshop.coupon.service.MemberPriceService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("memberPriceService")
public class MemberPriceServiceImpl extends ServiceImpl<MemberPriceDao, MemberPriceEntity> implements MemberPriceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberPriceEntity> page = this.page(
                new Query<MemberPriceEntity>().getPage(params),
                new QueryWrapper<MemberPriceEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * 批量保存会员价格信息
     * @param skuId
     * @param memberPriceList
     */
    @Override
    public void saveMemberPriceBatch(Long skuId, List<MemberPrice> memberPriceList) {
        if (memberPriceList != null && memberPriceList.size() > 0){
            List<MemberPriceEntity> memberPriceEntities = memberPriceList.stream().map(memberPrice -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuId);
                memberPriceEntity.setMemberLevelId(memberPrice.getId());
                memberPriceEntity.setMemberLevelName(memberPrice.getName());
                memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                return memberPriceEntity;
            }).filter(memberPrice -> {
                return memberPrice.getMemberPrice().compareTo(BigDecimal.ZERO) == 1;
            }).collect(Collectors.toList());

            this.saveBatch(memberPriceEntities);
        }
    }

}
